package cn.b.sky.mes.hr.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.hr.dao.PerformanceDao;
import cn.b.sky.mes.hr.dao.ShiftingDao;
import cn.b.sky.mes.hr.model.Performance;
import cn.b.sky.mes.hr.model.Shifting;
import cn.b.sky.mes.hr.model.condition.ShiftingCondition;
import cn.b.sky.mes.hr.service.ShiftingService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

@Service
public class ShiftingServiceImpl implements ShiftingService {
  @Autowired
  ShiftingDao shiftingDao;

  @Autowired
  PerformanceDao performanceDao;

  @Override
  public List<Shifting> getShiftingsListByCondition(ShiftingCondition condition) {
    return shiftingDao.getShiftingsListByCondition(condition);
  }

  @Override
  public int getShiftingsCountByCondition(ShiftingCondition condition) {
    return shiftingDao.getShiftingsCountByCondition(condition);
  }

  @Override
  public Shifting getShiftingById(Long id) {
    if (null == id) {
      id = 0L;
    }
    return shiftingDao.getShiftingById(id);
  }

  @Override
  public void saveShifting(Shifting shifting, boolean isShiftingAble) {
    if (null != shifting.getId() && 0 != shifting.getId()) {
      shiftingDao.updateShifting(shifting);
      if (isShiftingAble) {
        performanceDao.updatePerformanceByShifting(shifting);
      }
    } else {
      shiftingDao.insertShifting(shifting);
    }
  }

  @Override
  public List<Shifting> getShiftingByPersonnelAndDate(String siteCode, String personnelCode, String theDate, Long id) {
    if (null == id) {
      id = 0L;
    }
    return shiftingDao.getShiftingByPersonnelAndDate(siteCode, personnelCode, theDate, id);
  }

  @Override
  public List<Shifting> getNotPerformancesShiftingByPersonnelAndDate(String siteCode, String personnelCode,
      String theDate) {
    return shiftingDao.getNotPerformancesShiftingByPersonnelAndDate(siteCode, personnelCode, theDate);
  }

  @Override
  public List<Shifting> getShiftingByPersonnelAndPeriod(String siteCode, String personnelCode, String theDate, Long id) {
    if (null == id) {
      id = 0L;
    }
    return shiftingDao.getShiftingByPersonnelAndPeriod(siteCode, personnelCode, theDate, id);
  }

  @Override
  public BatchJobReport deleteShiftingById(Long id, boolean isShiftingAble) {
    boolean success = true;
    String languageCode = SecurityContextUtil.getLanguageCode();
    String msg = "";
    if (null == id) {
      id = 0L;
    }
    Performance performance = performanceDao.selectPerformanceByShiftingId(id, null);
    if (isShiftingAble && null != performance) {
      success = false;
      msg = performance.getPersonnelName();
    }
    if (success) {
      shiftingDao.deleteShifting(id);
    }
    return new BatchJobReport(msg, LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),languageCode,
        "dataBaseOperationPkFail.M") + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),languageCode, "notDelete.M"),
        success, languageCode, SecurityContextUtil.getSiteCode());
  }

  @Override
  public List<Shifting> getShiftingsByWorkcenterAndDate(String siteCode, String workcenterCode, String theDate) {
    return shiftingDao.getShiftingsByWorkcenterAndDate(siteCode, workcenterCode, theDate);
  }

  @Override
  public List<Shifting> getNotPerformancesShiftingByWorkcenterAndDate(String siteCode, String workCenterCode,
      String theDate) {
    if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(workCenterCode) || StringUtils.isBlank(theDate)) {
      return null;
    }
    return shiftingDao.getNotPerformancesShiftingByWorkcenterAndDate(siteCode, workCenterCode, theDate);
  }

  @Override
  public Date getLatestAnnangedDate(String siteCode, String workcenterCode) {
    return shiftingDao.getLatestAnnangedDate(siteCode, workcenterCode);
  }

}
